package com.dlc.shop.multishop.controller;

import cn.hutool.core.util.BooleanUtil;
import com.dlc.shop.bean.model.ShopTemplate;
import com.dlc.shop.common.config.Constant;
import com.dlc.shop.common.exception.YamiShopBindException;
import com.dlc.shop.common.i18n.I18nMessage;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.security.multishop.util.SecurityUtils;
import com.dlc.shop.service.ShopTemplateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Objects;


/**
 *
 *
 * @author LGH
 * @date 2022-07-29 16:54:02
 */
@RestController
@RequestMapping("/shop/shopTemplate")
@Tag(name = "店铺模板-修改、删除、设为主页")
@RequiredArgsConstructor
public class ShopTemplateUpDelController {

    private final ShopTemplateService shopTemplateService;
    @Value("${yami.expose.operation.auth:}")
    private Boolean permission;

    /**
     * 修改店铺模板信息
     * @param shopTemplate 店铺模板信息
     * @return 是否修改成功
     */
    @PutMapping("/updatePC")
    @PreAuthorize("@pms.hasPermission('shop:shopTemplate:updatePC')")
    @Operation(summary = "PC端修改店铺模板信息" , description = "PC端修改店铺模板信息")
    public ServerResponseEntity<Boolean> updatePcById(@RequestBody @Valid ShopTemplate shopTemplate) {
        if (BooleanUtil.isFalse(permission) && Objects.equals(SecurityUtils.getShopUser().getShopId(), Constant.MAIN_SHOP)) {
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.operate.auth"));
        }
        ShopTemplate shopTemplateDb = shopTemplateService.getById(shopTemplate.getTemplateId());
        if (Objects.isNull(shopTemplateDb)) {
            //查找不到该模板信息
            throw new YamiShopBindException("yami.template.not.exist");
        }
        if(!Objects.equals(shopTemplateDb.getShopId() , SecurityUtils.getShopUser().getShopId())){
            // 没有权限进行操作
            throw new YamiShopBindException("yami.no.auth");
        }
        shopTemplate.setUpdateTime(new Date());
        return ServerResponseEntity.success(shopTemplateService.updateById(shopTemplate));

    }

    /**
     * 修改店铺模板信息
     * @param shopTemplate 店铺模板信息
     * @return 是否修改成功
     */
    @PutMapping("/updateMove")
    @PreAuthorize("@pms.hasPermission('shop:shopTemplate:updateMove')")
    @Operation(summary = "移动端修改店铺模板信息" , description = "移动端修改店铺模板信息")
    public ServerResponseEntity<Boolean> updateMoveById(@RequestBody @Valid ShopTemplate shopTemplate) {
        if (BooleanUtil.isFalse(permission) && Objects.equals(SecurityUtils.getShopUser().getShopId(), Constant.MAIN_SHOP)) {
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.operate.auth"));
        }
        ShopTemplate shopTemplateDb = shopTemplateService.getById(shopTemplate.getTemplateId());
        if (Objects.isNull(shopTemplateDb)) {
            //查找不到该模板信息
            throw new YamiShopBindException("yami.template.not.exist");
        }
        if(!Objects.equals(shopTemplateDb.getShopId() , SecurityUtils.getShopUser().getShopId())){
            // 没有权限进行操作
            throw new YamiShopBindException("yami.no.auth");
        }
        shopTemplate.setUpdateTime(new Date());
        return ServerResponseEntity.success(shopTemplateService.updateById(shopTemplate));

    }


    /**
     * 通过id删除店铺模板信息
     * @param templateId id
     * @return 是否删除成功
     */
    @DeleteMapping("/deletePC/{templateId}")
    @PreAuthorize("@pms.hasPermission('shop:shopTemplate:deletePC')")
    @Operation(summary = "PC端通过id删除店铺模板信息" , description = "PC端通过id删除店铺模板信息")
    @Parameter(name = "templateId", description = "模板id" )
    public ServerResponseEntity<Boolean> removePcById(@PathVariable Long templateId) {
        if (BooleanUtil.isFalse(permission) && Objects.equals(SecurityUtils.getShopUser().getShopId(), Constant.MAIN_SHOP)) {
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.operate.auth"));
        }
        ShopTemplate shopTemplate = shopTemplateService.getById(templateId);
        if(!Objects.equals(shopTemplate.getShopId() ,SecurityUtils.getShopUser().getShopId())){
            // 没有权限进行操作
            throw new YamiShopBindException("yami.no.auth");
        }
        return ServerResponseEntity.success(shopTemplateService.removeById(templateId));
    }

    /**
     * 通过id删除店铺模板信息
     * @param templateId id
     * @return 是否删除成功
     */
    @DeleteMapping("/deleteMove/{templateId}")
    @PreAuthorize("@pms.hasPermission('shop:shopTemplate:deleteMove')")
    @Operation(summary = "移动端通过id删除店铺模板信息" , description = "移动端通过id删除店铺模板信息")
    @Parameter(name = "templateId", description = "模板id" )
    public ServerResponseEntity<Boolean> removeMoveById(@PathVariable Long templateId) {
        if (BooleanUtil.isFalse(permission) && Objects.equals(SecurityUtils.getShopUser().getShopId(), Constant.MAIN_SHOP)) {
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.operate.auth"));
        }
        ShopTemplate shopTemplate = shopTemplateService.getById(templateId);
        if(!Objects.equals(shopTemplate.getShopId() ,SecurityUtils.getShopUser().getShopId())){
            // 没有权限进行操作
            throw new YamiShopBindException("yami.no.auth");
        }
        return ServerResponseEntity.success(shopTemplateService.removeById(templateId));
    }
}
